package org.openmrs.module.simplelabentry.web.dwr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openmrs.Concept;

class NameComparator implements Comparator<Node>{

	@Override
	public int compare(Node o1, Node o2) {
		return o1.getName().compareToIgnoreCase(o2.getName()); 
	}
	
}
public class Node {
	private Integer level = 0;
	private Concept concept;
	private List<LabOrderListItem> items = null;
	private Node parent = null;
	private String name;
    private String result;
    private String unit;
    private String lowNormal;
    private String highNormal;
	private Set<Node> childNodes = new HashSet<Node>();	
	
	
	public Node(){
		
	}

	public Node(Integer level, Concept concept, Node parent) {
		super();
		this.level = level;
		this.concept = concept;
		this.parent = parent;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Concept getConcept() {
		return concept;
	}

	public void setConcept(Concept concept) {
		this.concept = concept;
	}
	
	public List<LabOrderListItem> getItems() {
		return items;
	}
	
	public void setItems(List<LabOrderListItem> items) {
		this.items = items;
	}
	
	public Node getParent() {
		return parent;
	}
	
	public void setParent(Node parent) {
		this.parent = parent;
	}
	
	public Set<Node> getChildNodes() {				
		return childNodes;
	}
	
	public void setChildNodes(Set<Node> childNodes) {
		this.childNodes = childNodes;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getLowNormal() {
		return lowNormal;
	}

	public void setLowNormal(String lowNormal) {
		this.lowNormal = lowNormal;
	}

	public String getHighNormal() {
		return highNormal;
	}

	public void setHighNormal(String highNormal) {
		this.highNormal = highNormal;
	}
	
	public List<Node> getSortedChildNodes(){
		List<Node> nodes = new ArrayList<Node>();
		nodes.addAll(childNodes);		
		System.out.println("nodes.size() ==> " + nodes.size());
		Collections.sort(nodes, new NameComparator());
		return nodes;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((level == null) ? 0 : level.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Node other = (Node) obj;
		if (level == null) {
			if (other.level != null)
				return false;
		} else if (!level.equals(other.level))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
}
